from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from .. models import Goods,Types
from . typeviews import typegetall
import time,os
# Create your views here.
# 商品列表
def goodsindex(request):
	from django.core.paginator import Paginator

	v = request.GET.get('keywords','')

	arr = {
		1:['新品','新'],
		2:['热销','热'],
		3:['下架','下']
	}
	status=0
	for k,x in arr.items():
		if v in x:
			status = k
	# Q对象
	from django.db.models import Q

	ob = Goods.objects.filter(Q(id__contains=v)|Q(title__contains=v)|Q(typeid__name__contains=v)|Q(price__contains=v)|Q(status__contains=status))

	 # 实例化分页类
	paginator = Paginator(ob,2)

	# 获取当前页码
	p = int(request.GET.get('p',1))

	# 获取分页数据对象
	userlist= paginator.page(p)

	# 分配数据

	context = {'goods':userlist,'p':p}


	return render(request,'myadmin/goods/index.html',context)

# 商品添加
def goodsadd(request):
	# 获取所有的分类 
    ob = typegetall()

    # 分配数据
    context = {'typelist':ob}


    # 加载模板
    return render(request,'myadmin/goods/add.html',context)

# 添加处理
def goodsinsert(request):
	try:

		myfile = request.FILES.get('pic',None)
		if not myfile:
			return HttpResponse('<script>alert("没有文件上传");location.href="/admin/goodsadd"</script>')
		   
		ob = Goods()

		ob.typeid = Types.objects.get(id=request.POST['typeid'])
		ob.title = request.POST['title']
		ob.price = request.POST.get('price')
		ob.storage = request.POST.get('storage')
		ob.info = request.POST.get('content')
		ob.status = request.POST.get('status')
		ob.pic = upload(request)
		ob.save()

		return HttpResponse('<script>alert("添加成功");location.href="/admin/goodsadd"</script>')
	except:
		return HttpResponse('<script>alert("添加失败");location.href="/admin/goodsadd"</script>')


# 商品删除
def goodsdel(request,gid):
	# return HttpResponse('1')
	ob = Goods.objects.get(id=gid)
	os.remove('static/public/img/'+str(ob.pic))

	ob.delete()
	
	return HttpResponse('<script>alert("删除成功");location.href="/admin/goodsindex"</script>')

# 商品修改
def goodsedit(request,gid):
	ob = Goods.objects.get(id=gid)
	
	return render(request,'myadmin/goods/edit.html',{'ginfo':ob})

# 修改处理
def goodsupdate(request):
	
	try:
		ob = Goods.objects.get(id=request.POST['gid'])

		ob.title = request.POST['title']
		ob.price = request.POST.get('price')
		ob.storage = request.POST.get('storage')
		ob.info = request.POST.get('content')
		ob.status = request.POST.get('status')
		ob.pic = upload(request)
		myfile = request.FILES.get('pic',None)
		if myfile:
			
			os.remove('./static/public/img/'+ob.pic)
			ob.pic = upload(request)

		ob.save()

	# return HttpResponse('1')
		
		return HttpResponse('<script>alert("修改成功");location.href="/admin/goodsindex"</script>')
	except:
		return HttpResponse('<script>alert("修改失败");location.href="/admin/goodsedit/'+ob.id+'"</script>')






def upload(request):
	#执行图片的上传
	myfile = request.FILES.get("pic")
	# print(myfile)
	filename = str(time.time())+"."+myfile.name.split('.').pop()
	print(filename)

	destination = open("./static/public/img/"+filename,"wb+")
	for chunk in myfile.chunks():      # 分块写入文件  
	    destination.write(chunk)  
	destination.close()
	# 返回上传的文件名
	return filename
